package com.ziyun.erp.modules.freightages.service.impl;


import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.utility.New;
import com.ziyun.erp.modules.freightages.AreaVo;
import com.ziyun.erp.modules.freightages.dao.SdbEctoolsRegionsDao;
import com.ziyun.erp.modules.freightages.service.FreightTemplateService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.freightages.entity.SdbEctoolsRegionsEntity;
import com.ziyun.erp.modules.freightages.service.SdbEctoolsRegionsService;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("sdbEctoolsRegionsService")
public class SdbEctoolsRegionsServiceImpl extends ServiceImpl<SdbEctoolsRegionsDao, SdbEctoolsRegionsEntity> implements SdbEctoolsRegionsService {

    @Autowired
    private SdbEctoolsRegionsDao sdbEctoolsRegionsDao;

    @Autowired
    private FreightTemplateService freightTemplateService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SdbEctoolsRegionsEntity> page = this.selectPage(
                new Query<SdbEctoolsRegionsEntity>(params).getPage(),
                new EntityWrapper<SdbEctoolsRegionsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SdbEctoolsRegionsEntity> findRoots() {
        return sdbEctoolsRegionsDao.findRoots();
    }

    @Override
    public List<SdbEctoolsRegionsEntity> queryListByParentId(int regionId) {
        return sdbEctoolsRegionsDao.queryListByParentId(regionId);
    }

    @Override
    public List<String> queryRegionsSascade() {
        return this.baseMapper.queryRegionsSascade();
    }

    @Override
    public List<String> queryRegionsList() {
        return this.baseMapper.queryRegionsList();
    }


    @Override
    public SdbEctoolsRegionsEntity selectInfoById(String contactAreaId) {
        return sdbEctoolsRegionsDao.selectInfoById(contactAreaId);
    }

    @Override
    public R getUserData(Map<String, Object> params) {
        PageUtils page = freightTemplateService.queryPage(params);
        List<SdbEctoolsRegionsEntity> regionsListRoot = this.findRoots();
        JSONArray jsonArray = new JSONArray();
        for (SdbEctoolsRegionsEntity egions : regionsListRoot) {
            JSONArray jsonArrayChild = new JSONArray();
            JSONObject json = new JSONObject();
            json.put("id", egions.getRegionId());
            json.put("name", egions.getLocalName());
            List<SdbEctoolsRegionsEntity> listChild = this
                    .queryListByParentId(egions.getRegionId());
            for (SdbEctoolsRegionsEntity areaChild : listChild) {
                JSONObject jsonChild = new JSONObject();
                jsonChild.put("id", areaChild.getRegionId());
                jsonChild.put("name", areaChild.getLocalName());
                jsonArrayChild.add(jsonChild);
            }
            json.put("sub", jsonArrayChild);
            jsonArray.add(json);
        }
        return R.ok().put("page", page).put("userData", jsonArray);
    }

    @Override
    public List<Map> getAllRegions() {
        Map<String, Object> map = New.hashMap();
        map.put("disabled", "false");
        List<SdbEctoolsRegionsEntity> sdbEctoolsRegionsEntities = sdbEctoolsRegionsDao.selectByMap(map);
        List<Map> resultList = regionsTreeHandler(sdbEctoolsRegionsEntities, 0);

        return resultList;
    }

    private List<Map> regionsTreeHandler(List<SdbEctoolsRegionsEntity> sdbEctoolsRegionsEntities, int i) {
        List<Map> resultList = New.arrayList();
        //首先获得根节点数据
        for (SdbEctoolsRegionsEntity sdbEctoolsRegionsEntity : sdbEctoolsRegionsEntities) {

            if (sdbEctoolsRegionsEntity.getPRegionId() == i) {
                Map<String, Object> map = New.hashMap();
                map.put("value", sdbEctoolsRegionsEntity.getRegionId());
                map.put("label", sdbEctoolsRegionsEntity.getLocalName());
                resultList.add(map);
            }
        }
        for (Map map : resultList) {
            int regionId = Integer.parseInt(map.get("value").toString());
            List<Map> temp = regionsTreeHandler(sdbEctoolsRegionsEntities, regionId);
            if (temp.size() > 0) {
                map.put("children", temp);
            }
        }
        return resultList;
    }
}
